﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Urbini.Fabio._5BI.EquazioniDifferenziali
{
    public partial class frmDecadimento : Form
    {
        string OnlyNumbers(string input)
        {
            string result = "";
            foreach (char c in input)
                if ("0123456789".IndexOf(c) >= 0) result += c;
            return result;
        }
        string DoubleNumbers(string input, bool ControlloNegativo)
        {
            string result = "";
            if (ControlloNegativo)
            {
                foreach (char c in input)
                    if ("0123456789,-".IndexOf(c) >= 0) result += c;
            }
            else
                foreach (char c in input)
                    if ("0123456789,".IndexOf(c) >= 0) result += c;

            return result;
        }
        void StringaDouble(TextBox sender, bool ControlloNegativo)
        {
            // metodo per restituire una stringa convertibile in double

            TextBox t = (TextBox)sender;
            t.Text = DoubleNumbers(t.Text, ControlloNegativo);

            char c = ',';

            if (t.Text.Contains(c))
            {
                string sx = "", dx = "";
                int idx = t.Text.IndexOf(c);
                string temp = "";

                // sx contiene il numero più la virgola. 
                for (int i = 0; i < idx + 1; i++) sx += t.Text[i];

                for (int i = idx + 1; i < t.Text.Length; i++) temp += t.Text[i];

                foreach (char ch in OnlyNumbers(temp)) dx += ch;
                t.Text = sx + dx;
            }
            // Controllo sul segno negativo:
            if (ControlloNegativo)
                if (t.Text.Contains('-'))
                    for (int i = 1; i < t.Text.Length; i++)
                        if (t.Text[i] == '-')
                        {
                            string text = "";
                            for (int j = 0; j < i; j++)
                                text += t.Text[j];
                            t.Text = text;
                        }
        }
        double AcquisizioneDati(TextBox txt)
        {
            try
            {
                double temp = Convert.ToDouble(txt.Text);
                if (temp >= 0) return temp;
                else throw new Exception();
            }
            catch
            {

            }
            return 0;
        }

        // Qui inizia tutto
        public frmDecadimento()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Reset();
        }

        private void txtIntervalli_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            t.Text = OnlyNumbers(t.Text);
        }

        private void txt_Changed(object sender, EventArgs e)
        {
            StringaDouble((TextBox)sender, false);
        }

        private void btnAvvia_Click(object sender, EventArgs e)
        {
            double n0 = AcquisizioneDati(txtn0);
            double t12 = AcquisizioneDati(txtt12);
            double timeElapsed = AcquisizioneDati(txtTimeElapsed);
            int Intervalli = Convert.ToInt32(txtIntervalli.Text + 0);

            EDNuclei c = new EDNuclei(n0, t12, timeElapsed, Intervalli);
            (new frmTabella(c.GetXY())).ShowDialog();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        void Reset()
        {
            txtn0.Text = "100";
            txtt12.Text = "5730";
            txtTimeElapsed.Text = "20000";
            txtIntervalli.Text = "100";
        }
    }
}
